/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* E.P. Scene Graph
* Developed by:
*       - Eduardo Ribeiro (eribeiro @ tecgraf . puc-rio . br)
*       - Peter Dam (peter @ tecgraf . puc-rio . br)
* File: Array.h
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef _EPSG_VEC4_
#define _EPSG_VEC4_

#include <vector>
#include <stdlib.h>

namespace EPSG
{
	template <typename T>
	class Array
	{
	public:
		/* [CONSTRUCTOR] */	Array();

		T&					operator[]( int index );
		void				push_back( const T& val );

		T*					asStandardArray();

	private:
		T					_invalid;
		std::vector<T>		_array;
		T*					_stdArr;
	};

	template <typename T>
	EPSG::Array<T>::Array()
	{
		_stdArr = NULL;
	}

	/* TEMPLATE METHODS */
	template <typename T>
	T& EPSG::Array<T>::operator[]( int index )
	{
		if( index > _array.size() || index < 0 )
			return _invalid;

		return _array.at( index );
	}

	template <typename T>
	T* EPSG::Array<T>::asStandardArray()
	{
		if( _stdArr != NULL )
		{
			free( _stdArr );
			_stdArr = NULL;
		}

		_stdArr = (T*)malloc( sizeof(T) * _array.size() );
		for( int i = 0; i < _array.size(); i++ )
			_stdArr[i] = _array.at(i);

		return _stdArr;
	}
}

#endif //_EPSG_VEC4_